package qianfeng.com.meijiadaka;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.lemon.view.RefreshRecyclerView;
import cn.lemon.view.adapter.Action;
import qianfeng.com.meijiadaka.adapter.xiaojian.XJAdapter;
import qianfeng.com.meijiadaka.bean.TutorialBean;
import qianfeng.com.meijiadaka.bean.xiaojian.XiaoJianBean;
import qianfeng.com.meijiadaka.interfacebean.TutorialData;
import qianfeng.com.meijiadaka.interfacebean.xiaojian.XiaoJianData;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class XiaoJianActivity extends AppCompatActivity {

    @BindView(R.id.xiaojian_rrv)
    RefreshRecyclerView mRecyclerView;
    private String mUri = "http://home.naildaka.com/vapi/v3/home/goods/recommend?pageNo=1";
    private XJAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xiao_jian);
        ButterKnife.bind(this);
        init();

    }

    private void init() {
        mAdapter = new XJAdapter(this);
        mRecyclerView.setSwipeRefreshColors(0xFF437845, 0xFFE44F98, 0xFF2FAC21);
        mRecyclerView.setLayoutManager(new LinearLayoutManager((this)));
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setRefreshAction(new Action() {
            @Override
            public void onAction() {
            loadData();
            }
        });
        mRecyclerView.post(new Runnable() {
            @Override
            public void run() {
           loadData();
            }
        });
    }

    private void loadData() {
        new Retrofit.Builder().baseUrl("http://home.naildaka.com/").addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
                addConverterFactory(GsonConverterFactory.create()).build().create(XiaoJianData.class).getData(mUri).
                observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Action1<XiaoJianBean>() {
            @Override
            public void call(XiaoJianBean xiaoJianBean) {
                mAdapter.clear();
                mAdapter.addAll(xiaoJianBean.getResult());
                mRecyclerView.dismissSwipeRefresh();
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {

            }
        });
    }

    @OnClick({R.id.xiaojian_ib})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.xiaojian_ib:
                finish();
                break;

        }
    }
}
